package com.restaurant.ressys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.restaurant.ressys.entity.Food;
import com.restaurant.ressys.entity.OrderItem;
import com.restaurant.ressys.service.FoodService;
import com.restaurant.ressys.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/orderItem")
public class OrderItemController {
    @Autowired
    private OrderItemService ois;
    @Autowired
    private FoodService fs;
    @GetMapping("/selectAll")
    public List<OrderItem> selectAll(){
        return ois.list();
    }
    @PostMapping("/add")
    public void add(@RequestBody OrderItem orderItem){
        ois.save(orderItem);
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        ois.removeById(id);
    }
    @GetMapping("/selectByOid/{oid}")
    public List<OrderItem> selectByOid(@PathVariable("oid") Integer oid){
        QueryWrapper<OrderItem> wrapper=new QueryWrapper<>();
        wrapper.eq("oid",oid);
        List<OrderItem> list = ois.list(wrapper);
        return list.stream().peek((oi)->{
            Food food = fs.getById(oi.getFid());
            oi.setFood(food);
        }).collect(Collectors.toList());
    }
}

